#include "Platform/OpenGL/openGLUniformBuffer.h"

// #include <glad/glad.h>

namespace CC {

	OpenGLUniformBuffer::OpenGLUniformBuffer(unsigned int size, std::string name, uint32_t programID)
	{
		glGenBuffers(1, &m_RendererID);
		// CC_CORE_INFO("OpenGLUniformBuffer m_RendererID = %d;size=%d\n", m_RendererID, size);
		// glNamedBufferData(m_RendererID, size, nullptr, GL_DYNAMIC_DRAW); // TODO: investigate usage hint
		glBindBuffer(GL_UNIFORM_BUFFER, m_RendererID);
		glBufferData(GL_UNIFORM_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);
		
		// CC_CORE_INFO("OpenGLUniformBuffer glGetUniformBlockIndex programID= %d\n", programID);
		unsigned int binding = glGetUniformBlockIndex(programID, name.c_str());
		// CC_CORE_INFO("OpenGLUniformBuffer = %d\n", binding);

		// int uboSize;
		// glGetActiveUniformBlockiv(programID, binding, GL_UNIFORM_BLOCK_DATA_SIZE, &uboSize);
		// CC_CORE_INFO("glGetActiveUniformBlockiv = %d;size=%d\n", uboSize, size);

		glBindBufferBase(GL_UNIFORM_BUFFER, binding, m_RendererID);
	}

	OpenGLUniformBuffer::~OpenGLUniformBuffer()
	{
		glDeleteBuffers(1, &m_RendererID);
	}

	void OpenGLUniformBuffer::SetData(const void* data, unsigned int size, unsigned int offset)
	{
		// glNamedBufferSubData(m_RendererID, offset, size, data);
		glBindBuffer(GL_UNIFORM_BUFFER, m_RendererID);
		glBufferSubData(GL_UNIFORM_BUFFER, offset, size, data);
	}

}
